package com.xcy.quickhand.spider;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.xcy.quickhand.model.A;
import com.xcy.quickhand.model.AuthorInfo;
import com.xcy.quickhand.model.Video;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description 爬取我的关注的作者工具类
 * @date 2023/2/12 21:31
 */
public class CrawlAuthorUtils {

    public static void main(String[] args) {
        String cookie = "did=web_1dbcb1fddff24c74944acecb8029726d; didv=1675945966000; kpf=PC_WEB; clientid=3; userId=734799445; kpn=KUAISHOU_VISION; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqAB7GbHXdHY0D84t1ywGHlNdP-d1IbeZ4S243HzXP4Xtv0lFlwDcRR0hVtUJ_t3ov8nhP52KLFP36Uwxxu8J2mSififhJ-ThZyu3281dvL2ughvhm_-87gcOeCVSX1lcNvyda34Amf_rL7otRII6HxdR-rKTEo6XTXtMyaPKW9JfabLu6zXXUsRBFTIhljVmycRg0e-1vKA5pI_jEmJBPfbThoS6uws2LN-siMyPVYdMaXTUH7FIiDXww3DMBNACt_oAz5m9AVV6QC5QozAw-Csc_m41G3uRigFMAE; kuaishou.server.web_ph=08b0d26282595f72b5d4da4bd3d01ce1c0e0";
        crawlVideoListByAuthorId(cookie, "11");
    }

    // 爬取作者的所有视频
    public static List<AuthorInfo> crawlVideoListByAuthorId(String cookie, String userId) {
        List<AuthorInfo> pageList = crawlPageAuthor("", cookie, userId, 1);
        return pageList;
    }


    private static List<AuthorInfo> crawlPageAuthor(String cursor, String cookie, String userId, Integer page) {
        String url = "https://www.kuaishou.com/graphql";

        System.out.println("正在爬取的页码: " + page);

        // 所有的视频作品
        List authorList = new ArrayList<AuthorInfo>();

        String response = HttpRequest.post(url).header("cookie", cookie)
                .header("content-type", "application/json")
                .header("Host", "www.kuaishou.com")
                .header("Origin", "https://www.kuaishou.com")
                .header("Referer", "https://www.kuaishou.com/myFollow")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                .body(
                        "{\n" +
                                "  \"operationName\": \"likeDataQuery\",\n" +
                                "  \"variables\": {\n" +
                                "    \"token\": \"4151162645f5fb717b1c1f1e6d66e88685edef65000002020d0c0f15\",\n" +
                                "    \"st\": 1676208512131,\n" +
                                "    \"page\": \"follow\",\n" +
                                "    \"pcursor\": \"61\"\n" +
                                "  },\n" +
                                "  \"query\": \"fragment photoContent on PhotoEntity {\\n  id\\n  duration\\n  caption\\n  originCaption\\n  likeCount\\n  viewCount\\n  realLikeCount\\n  coverUrl\\n  photoUrl\\n  photoH265Url\\n  manifest\\n  manifestH265\\n  videoResource\\n  coverUrls {\\n    url\\n    __typename\\n  }\\n  timestamp\\n  expTag\\n  animatedCoverUrl\\n  distance\\n  videoRatio\\n  liked\\n  stereoType\\n  profileUserTopPhoto\\n  musicBlocked\\n  __typename\\n}\\n\\nfragment feedContent on Feed {\\n  type\\n  author {\\n    id\\n    name\\n    headerUrl\\n    following\\n    headerUrls {\\n      url\\n      __typename\\n    }\\n    __typename\\n  }\\n  photo {\\n    ...photoContent\\n    __typename\\n  }\\n  canAddComment\\n  llsid\\n  status\\n  currentPcursor\\n  tags {\\n    type\\n    name\\n    __typename\\n  }\\n  __typename\\n}\\n\\nfragment photoResult on PhotoResult {\\n  result\\n  llsid\\n  expTag\\n  serverExpTag\\n  pcursor\\n  feeds {\\n    ...feedContent\\n    __typename\\n  }\\n  webPageArea\\n  __typename\\n}\\n\\nquery likeDataQuery($pcursor: String, $page: String, $token: String, $st: Long) {\\n  likeData(pcursor: $pcursor, page: $page, token: $token, st: $st) {\\n    ...photoResult\\n    __typename\\n  }\\n}\\n\"\n" +
                                "}"
                ).execute().body();


        //"query": "fragment photoContent on PhotoEntity {\n  id\n  duration\n  caption\n  originCaption\n  likeCount\n  viewCount\n  realLikeCount\n  coverUrl\n  photoUrl\n  photoH265Url\n  manifest\n  manifestH265\n  videoResource\n  coverUrls {\n    url\n    __typename\n  }\n  timestamp\n  expTag\n  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\n  musicBlocked\n  __typename\n}\n\nfragment feedContent on Feed {\n  type\n  author {\n    id\n    name\n    headerUrl\n    following\n    headerUrls {\n      url\n      __typename\n    }\n    __typename\n  }\n  photo {\n    ...photoContent\n    __typename\n  }\n  canAddComment\n  llsid\n  status\n  currentPcursor\n  tags {\n    type\n    name\n    __typename\n  }\n  __typename\n}\n\nfragment photoResult on PhotoResult {\n  result\n  llsid\n  expTag\n  serverExpTag\n  pcursor\n  feeds {\n    ...feedContent\n    __typename\n  }\n  webPageArea\n  __typename\n}\n\nquery likeDataQuery($pcursor: String, $page: String, $token: String, $st: Long) {\n  likeData(pcursor: $pcursor, page: $page, token: $token, st: $st) {\n    ...photoResult\n    __typename\n  }\n}\n"


        System.out.println(response);

        //A a = JSONUtil.toBean(response, A.class);
        //
        //// 下一页游标
        //String nextCursor = a.getData().getVisionProfilePhotoList().getPcursor();
        //
        //List<A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO> feeds = a.getData().getVisionProfilePhotoList().getFeeds();
        //
        //
        //for (int i = 0; i < feeds.size(); i++) {
        //    A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO feedsDTO = feeds.get(i);
        //    A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO.AuthorDTO author = feedsDTO.getAuthor();
        //
        //    // 作者id
        //    String authorId = author.getId();
        //    System.out.println("作者id " + authorId);
        //    // 作者头像
        //    String headerUrl = author.getHeaderUrl();
        //    System.out.println("作者头像 " + headerUrl);
        //    // 作者名称
        //    String authorName = author.getName();
        //    System.out.println("作者名称 " + authorName);
        //
        //    A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO.PhotoDTO photo = feedsDTO.getPhoto();
        //
        //    // 视频唯一id(待确认是否是全局唯一)
        //    String videoId = photo.getId();
        //    System.out.println("视频唯一id " + videoId);
        //    // 动态缩略图
        //    String animatedCoverUrl = photo.getAnimatedCoverUrl();
        //    System.out.println("动态缩略图 " + animatedCoverUrl);
        //    // 封面图
        //    String coverUrl = photo.getCoverUrl();
        //    System.out.println("封面图 " + coverUrl);
        //    // 标题
        //    String caption = photo.getCaption();
        //    System.out.println("标题 " + caption);
        //    // 视频地址高清
        //    String photoUrl = photo.getPhotoUrl();
        //    System.out.println("视频地址高清 " + photoUrl);
        //
        //    // 视频地址低清
        //    String photoH265Url = photo.getPhotoH265Url();
        //    System.out.println("视频地址低清 " + photoH265Url);
        //
        //    Video video = new Video();
        //    video.setVideoId(videoId);
        //    video.setAnimatedOverUrl(animatedCoverUrl);
        //    video.setCoverUrl(coverUrl);
        //    video.setVideoCaption(caption);
        //    video.setPhotoUrl(photoUrl);
        //    video.setPhotoH265Url(photoH265Url);
        //    video.setAuthorId(authorId);
        //    video.setHeaderUrl(headerUrl);
        //    video.setAuthorName(authorName);
        //    video.setCreateTime(new Date());
        //
        //    authorList.add(video);
        //}
        //
        //if (!nextCursor.equals("no_more")) {
        //    page = page + 1;
        //    List<Video> videos = crawlPageAuthor(nextCursor, cookie, userId, page);
        //    authorList.addAll(videos);
        //}

        return authorList;
    }

}
